package com.qf.exception.homework;

import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CatDemo {

    public static void main(String[] args) {
//        System.out.println(Cat.randomCat());

        List<Cat> cats = Cat.randomCats(10);
//        for (Cat cat : cats) {
//            System.out.println(cat);
//        }

        List<Integer> catAges = CatUtils.getCatAges(cats);
        System.out.println(catAges);

        // 利用Stream 实现集合中的对象类型的转换
        List<Integer> ageList = cats.stream().map(cat -> cat.getAge()).collect(Collectors.toList());
        System.out.println(ageList);

        List<Integer> ageList2 = cats.stream().map(Cat::getAge).collect(Collectors.toList());
        System.out.println(ageList2);

        List<String> nameList = cats.stream().map(Cat::getName).collect(Collectors.toList());
        System.out.println(nameList);

        // cat的年龄统计,总和、平均值、最大值、最小值
        IntSummaryStatistics statistics = cats.stream().mapToInt(Cat::getAge).summaryStatistics();
        System.out.println(statistics);

        // 过滤数据，只保留年龄大于2的cat，返回List
        List<Cat> catList = cats.stream().filter(cat -> cat.getAge() > 80).collect(Collectors.toList());
        System.out.println(catList);

        // 对cat进行分组，按照性别分组，返回Map<String,List<Cat>>
        Map<String, List<Cat>> map = cats.stream().collect(Collectors.groupingBy(Cat::getSex));
        System.out.println(map);

    }

}
